﻿using Ongoal.Quotation.Common;
using Ongoal.Quotation.Service;
using Ongoal.Quotation.Service.Common;
using Ongoal.Quotation.Service.Interface;
using Ongoal.Quotation.Service.Model;
using Ongoal.Quotation.Service.Model.Sys;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ongoal.Quotation.Service
{
    /// <summary>
    /// 数据项管理
    /// </summary>
    public class DataItemService : ServiceBase, IDataItemService
    {
        /// <summary>
        /// 获取数据项
        /// </summary>
        /// <returns></returns>
        public object GetDataItem(int catagoryid) {
            using (var db = Db.GetDbContext(string.Empty))
            {
             return db.Queryable<T_Sys_DataItem>().Where(z=>z.Deleted!=1 && z.CatatgoryID==catagoryid) .ToList();
            }
        }
        /// <summary>
        /// 新增数据项
        /// </summary>
        /// <returns></returns>
        public object AddDataItem(T_Sys_DataItem model)
        {
            try
            {
                checkdata(model, 1);
                this.Insert<T_Sys_DataItem>(model);
                return true;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        public bool checkdata(T_Sys_DataItem model,int checktype)
        {
            using (var db=Db.GetDbContext(""))
            {
                if (checktype == 1) //新增
                {
                    if (db.Queryable<T_Sys_DataItem>().Any(z => z.Code == model.Code && z.CatatgoryID == model.CatatgoryID && z.Deleted!=1))
                    {
                        throw new Exception("编码已经存在");
                    }
                    if (db.Queryable<T_Sys_DataItem>().Any(z => z.Name == model.Name && z.CatatgoryID == model.CatatgoryID && z.Deleted != 1))
                    {
                        throw new Exception("名称已经存在");
                    }
                }
                else { //编辑
                    if (db.Queryable<T_Sys_DataItem>().Any(z => z.Code == model.Code && z.CatatgoryID == model.CatatgoryID && z.ID!=model.ID && z.Deleted != 1))
                    {
                        throw new Exception("编码已经存在");
                    }
                    if (db.Queryable<T_Sys_DataItem>().Any(z => z.Name == model.Name && z.CatatgoryID == model.CatatgoryID && z.ID != model.ID && z.Deleted != 1))
                    {
                        throw new Exception("名称已经存在");
                    }
                }
                return true;

            }
        }
        /// <summary>
        /// 编辑数据项
        /// </summary>
        /// <returns></returns>
        public object EditDataItem(T_Sys_DataItem model)
        {
            try
            {
                checkdata(model, 2);
                this.Update<T_Sys_DataItem>(model);
                return true;
            }
            catch (Exception e)
            {


                throw new Exception(e.Message);
            }
        }



        /// <summary>
        /// 删除数据项
        /// </summary>
        /// <returns></returns>
        public object DeleteDataItem(List<int> Ids)
        {
            try
            {
                this.Delete<T_Sys_DataItem>(Ids.ToArray());
                return true;
            }
            catch (Exception e)
            {


                throw new Exception(e.Message);
            }
        }
    }
  
}
